package com.wust.tieshi;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.amo.demo.wheelview.NumericWheelAdapter;
import com.amo.demo.wheelview.OnWheelChangedListener;
import com.amo.demo.wheelview.WheelView;
import com.wust.R;
import com.wust.baby.BabyInfo;
import com.wust.baby.BabyInfoDao;
import com.wust.menu.ImageBtn;
import com.wust.util.SqltimeToTime;

public class record_UI extends Activity {
	private ImageBtn btn1=null;
	private Button record_date=null;
	private TextView record_age=null;
	private Button record_height=null;
	private Button record_weight=null;
	private Button record_chest=null;
	private Button record_headwidth=null;
	private Button save;
	private Button cancel;
	private EditText record_beizhu=null;
	private Dialog dialog;
	private SqltimeToTime timeTransfer;
	private BabyInfo info;
	private BabyInfoDao infoDao;
	private String heightstring,weigthstring,buststring,circumferencestring;
	private String baby_date;
	private String baby_id;
	private static int START_YEAR = 1990, END_YEAR = 2100;
	private Date birth;
	private long age;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.grow_add);
		SharedPreferences preferences = getSharedPreferences("userinfo",0);
		baby_id=preferences.getString("baby_id", "");
		info=new BabyInfo();
		timeTransfer=new SqltimeToTime();
		infoDao=new BabyInfoDao(this);
        record_date=(Button)findViewById(R.id.record_date);
        record_age=(TextView)findViewById(R.id.record_age);
        record_height=(Button)findViewById(R.id.record_height);
        record_weight=(Button)findViewById(R.id.record_weight);
        record_chest=(Button)findViewById(R.id.record_chest);
        record_headwidth=(Button)findViewById(R.id.record_headwidth);
        record_beizhu=(EditText)findViewById(R.id.record_beizhu);
        save=(Button)findViewById(R.id.record_save);
        save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				info.setInfo_height(Double.parseDouble(heightstring));
				info.setInfo_weigth(Double.parseDouble(weigthstring));
				info.setInfo_bust(Double.parseDouble(buststring));
				info.setInfo_circumference(Double.parseDouble(circumferencestring));
				info.setBaby_id(Integer.parseInt(baby_id));
				info.setInfo_date(baby_date);
				info.setDescription(record_beizhu.getText().toString());
				infoDao.insertBabyInfo(info);
				//Intent intent=new Intent(record_UI.this,charmActivity.class);
				//startActivity(intent);
				finish();
			}
		});
        record_date.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDateTimePicker();
			}
		});
        record_height.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showHeightPicker();
			}
		});
        record_weight.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showWeightPicker();
			}
		});
        record_chest.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showChestPicker();
			}
		});
        record_headwidth.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showHeadWidthPicker();
			}
		});
	}
/** 弹出身高选择器*/
	
	private void showHeightPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择身高");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.height_layout, null);
		
		final WheelView wv_height_z = (WheelView) view.findViewById(R.id.height_z);
		wv_height_z.setAdapter(new NumericWheelAdapter(0, 150));
		wv_height_z.setCyclic(true);
		wv_height_z.setCurrentItem(40);
		
		final WheelView wv_height_x=(WheelView)view.findViewById(R.id.height_x);
		wv_height_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_height_x.setCyclic(true);
		wv_height_x.setCurrentItem(00);
		wv_height_x.setLabel("  cm");
		
		int textsize=25;
		wv_height_z.TEXT_SIZE=textsize;
		wv_height_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_height_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_height_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);
				heightstring=wv_height_z.getCurrentItem()+"."+decimal.format(wv_height_x.getCurrentItem());
				record_height.setText(heightstring+"cm");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/** 体重选择器*/
	private void showWeightPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择体重");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.weight_layout, null);
		
		final WheelView wv_weight_z = (WheelView) view.findViewById(R.id.weight_z);
		wv_weight_z.setAdapter(new NumericWheelAdapter(0, 60));
		wv_weight_z.setCyclic(true);
		wv_weight_z.setCurrentItem(2);
		
		final WheelView wv_weight_x=(WheelView)view.findViewById(R.id.weight_x);
		wv_weight_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_weight_x.setCyclic(true);
		wv_weight_x.setCurrentItem(55);
		wv_weight_x.setLabel("  kg");
		
		int textsize=25;
		wv_weight_z.TEXT_SIZE=textsize;
		wv_weight_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_weight_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_weight_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);	
				weigthstring=wv_weight_z.getCurrentItem()+"."+decimal.format(wv_weight_x.getCurrentItem());
				record_weight.setText(weigthstring+"kg");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/** 胸围选择器*/
	private void showChestPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择胸围");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.chest_layout, null);
		
		final WheelView wv_chest_z = (WheelView) view.findViewById(R.id.chest_z);
		wv_chest_z.setAdapter(new NumericWheelAdapter(0, 60));
		wv_chest_z.setCyclic(true);
		wv_chest_z.setCurrentItem(29);
		
		final WheelView wv_chest_x=(WheelView)view.findViewById(R.id.chest_x);
		wv_chest_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_chest_x.setCyclic(true);
		wv_chest_x.setCurrentItem(43);
		wv_chest_x.setLabel("  cm");
		int textsize=25;
		wv_chest_z.TEXT_SIZE=textsize;
		wv_chest_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_chest_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_chest_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);	
				buststring=wv_chest_z.getCurrentItem()+"."+decimal.format(wv_chest_x.getCurrentItem());
				record_chest.setText(buststring+"cm");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/** 头围选择器*/
	private void showHeadWidthPicker(){
		dialog=new Dialog(this);
		dialog.setTitle("请选择头围");
		LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
		View view=inflater.inflate(R.layout.head_layout, null);
		
		final WheelView wv_headwidth_z = (WheelView) view.findViewById(R.id.headwidth_z);
		wv_headwidth_z.setAdapter(new NumericWheelAdapter(0, 60));
		wv_headwidth_z.setCyclic(true);
		wv_headwidth_z.setCurrentItem(29);
		
		final WheelView wv_headwidth_x=(WheelView)view.findViewById(R.id.headwidth_x);
		wv_headwidth_x.setAdapter(new NumericWheelAdapter(0, 99,".%02d"));
		wv_headwidth_x.setCyclic(true);
		wv_headwidth_x.setCurrentItem(43);
		wv_headwidth_x.setLabel("  cm");
		
		int textsize=25;
		wv_headwidth_z.TEXT_SIZE=textsize;
		wv_headwidth_x.TEXT_SIZE=textsize;
		Button btn_sure = (Button) view.findViewById(R.id.btn_headwidth_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_headwidth_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);	
				circumferencestring=wv_headwidth_z.getCurrentItem()+"."+decimal.format(wv_headwidth_x.getCurrentItem());
				record_headwidth.setText(circumferencestring+"cm");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	private void showDateTimePicker() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		

		// 添加大小月月份并将其转换为list,方便之后的判断
		String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
		String[] months_little = { "4", "6", "9", "11" };

		final List<String> list_big = Arrays.asList(months_big);
		final List<String> list_little = Arrays.asList(months_little);

		dialog = new Dialog(this);
		dialog.setTitle("请选择日期与时间");
		// 找到dialog的布局文件
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.time_layout, null);

		// 年
		final WheelView wv_year = (WheelView) view.findViewById(R.id.year);
		wv_year.setAdapter(new NumericWheelAdapter(START_YEAR, END_YEAR));// 设置"年"的显示数据
		wv_year.setCyclic(true);// 可循环滚动
		wv_year.setLabel("年");// 添加文字
		wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

		// 月
		final WheelView wv_month = (WheelView) view.findViewById(R.id.month);
		wv_month.setAdapter(new NumericWheelAdapter(1, 12));
		wv_month.setCyclic(true);
		wv_month.setLabel("月");
		wv_month.setCurrentItem(month);

		// 日
		final WheelView wv_day = (WheelView) view.findViewById(R.id.day);
		wv_day.setCyclic(true);
		// 判断大小月及是否闰年,用来确定"日"的数据
		if (list_big.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 31));
		} else if (list_little.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 30));
		} else {
			// 闰年
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				wv_day.setAdapter(new NumericWheelAdapter(1, 29));
			else
				wv_day.setAdapter(new NumericWheelAdapter(1, 28));
		}
		wv_day.setLabel("日");
		wv_day.setCurrentItem(day - 1);

		

		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String
						.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(wv_month
						.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);

		// 根据屏幕密度来指定选择器字体的大小
		int textSize = 0;

		textSize = 20;

		wv_day.TEXT_SIZE = textSize;
		wv_month.TEXT_SIZE = textSize;
		wv_year.TEXT_SIZE = textSize;

		Button btn_sure = (Button) view.findViewById(R.id.btn_datetime_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_datetime_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);
				baby_date=(wv_year.getCurrentItem() + START_YEAR) + "-"
						 + decimal.format((wv_month.getCurrentItem() + 1)) + "-"
						 + decimal.format((wv_day.getCurrentItem() + 1));
				// 设置日期的显示
			record_date.setText(baby_date);
				 SharedPreferences preferences = getSharedPreferences("userinfo",0);
				 String	birth_date=preferences.getString("baby_date","");
					System.out.println("date:"+baby_date+"birth:"+birth_date);
				 birth=timeTransfer.stringToDate(birth_date, "yyyy-MM-dd");
					Date date=timeTransfer.stringToDate(baby_date,"yyyy-MM-dd");	
					age=(date.getTime()-birth.getTime())/60/60/24/30/1000;
					record_age.setText(String.valueOf(age)+"月");
				dialog.dismiss();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		// 设置dialog的布局,并显示
		dialog.setContentView(view);
		dialog.show();
	}
}
